﻿using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace JilyCalc
{
    [Activity(Label = "计算器", MainLauncher = true, Icon = "@drawable/psb")]
    public class Activity1 : Activity
    {
        const string DefaultView = "0";
        const string Space = " ";

        const string puls = "+";
        const string minus = "-";
        const string multiply = "×";
        const string divide = "/";
        const string result = "=";

        string viewtext, viewtottle;
        string first, second;
        string actiontype;

        Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button0;
        Button buttonpuls, buttonminus, buttonmultiply, buttondivide, buttondot, buttonpercent;
        Button buttontottle, buttonclear, buttonsymbols, buttondelete;

        TextView textViewviewtext, textViewtottletext;

        bool isloaded = false;

        public bool IsFirst
        {
            get
            {
                return string.IsNullOrWhiteSpace(actiontype);
            }
        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            if (isloaded)
            {
                return;
            }

            isloaded = true;

            button1 = FindViewById<Button>(Resource.Id.button1); button1.Click += buttonnum_Click;
            button2 = FindViewById<Button>(Resource.Id.button2); button2.Click += buttonnum_Click;
            button3 = FindViewById<Button>(Resource.Id.button3); button3.Click += buttonnum_Click;
            button4 = FindViewById<Button>(Resource.Id.button4); button4.Click += buttonnum_Click;
            button5 = FindViewById<Button>(Resource.Id.button5); button5.Click += buttonnum_Click;
            button6 = FindViewById<Button>(Resource.Id.button6); button6.Click += buttonnum_Click;
            button7 = FindViewById<Button>(Resource.Id.button7); button7.Click += buttonnum_Click;
            button8 = FindViewById<Button>(Resource.Id.button8); button8.Click += buttonnum_Click;
            button9 = FindViewById<Button>(Resource.Id.button9); button9.Click += buttonnum_Click;
            button0 = FindViewById<Button>(Resource.Id.button0); button0.Click += buttonnum_Click;

            buttonpuls = FindViewById<Button>(Resource.Id.buttonpuls);
            buttonpuls.Click += (s, e) => { actiontype = puls; SetViewText(); this.textViewtottletext.Text = second.ToString(); };
            buttonminus = FindViewById<Button>(Resource.Id.buttonminus);
            buttonminus.Click += (s, e) => { actiontype = minus; SetViewText(); this.textViewtottletext.Text = second.ToString(); };
            buttonmultiply = FindViewById<Button>(Resource.Id.buttonmultiply);
            buttonmultiply.Click += (s, e) => { actiontype = multiply; SetViewText(); this.textViewtottletext.Text = second.ToString(); };
            buttondivide = FindViewById<Button>(Resource.Id.buttondivide);
            buttondivide.Click += (s, e) => { actiontype = divide; SetViewText(); this.textViewtottletext.Text = second.ToString(); };

            buttontottle = FindViewById<Button>(Resource.Id.buttontottle);
            buttontottle.Click += buttontottle_Click;
            buttonclear = FindViewById<Button>(Resource.Id.buttonclear);
            buttonclear.Click += buttonclear_Click;
            buttondelete = FindViewById<Button>(Resource.Id.buttondelete);
            buttondelete.Click += buttondelete_Click;

            buttonsymbols = FindViewById<Button>(Resource.Id.buttonsymbols);
            buttonsymbols.Click += buttonsymbols_Click;

            buttondot = FindViewById<Button>(Resource.Id.buttondot);
            buttonpercent = FindViewById<Button>(Resource.Id.buttonpercent);

            textViewviewtext = FindViewById<TextView>(Resource.Id.textViewviewtext);
            textViewtottletext = FindViewById<TextView>(Resource.Id.textViewtottletext);

            ViewClear();
        }

        void buttonnum_Click(object sender, EventArgs e)
        {
            var button = sender as Button;
            if (button != null)
            {
                double value = 0;
                if (double.TryParse(button.Text, out value))
                {
                    JudgeSetValue(button.Text);
                }
            }
        }

        void buttonclear_Click(object sender, EventArgs e)
        {
            ViewClear();
        }

        void buttontottle_Click(object sender, EventArgs e)
        {
            SetTottle();
        }

        void buttondelete_Click(object sender, EventArgs e)
        {
            if (IsFirst)
            {
                if (first.Length > 1)
                {
                    first = first.Remove(first.Length - 1, 1);
                }
                else
                {
                    first = DefaultView;
                }
                SetFirstNum(first, true);
            }
            else
            {
                if (second.Length > 1)
                {
                    second = first.Remove(second.Length - 1, 1);
                }
                else
                {
                    second = DefaultView;
                }
                SetSecondNum(second, true);
            }
        }

        void buttonsymbols_Click(object sender, EventArgs e)
        {
            if (IsFirst)
            {
                if (first.Length > 1)
                {
                    if (first[0].ToString().Equals(minus))
                    {
                        first = first.Replace(minus, string.Empty);
                    }
                    else
                    {
                        first = first.Insert(0, minus);
                    }
                    SetFirstNum(first, true);
                }
            }
            else
            {
                if (second.Length > 1)
                {
                    if (second[0].ToString().Equals(minus))
                    {
                        second = second.Replace(minus, string.Empty);
                    }
                    else
                    {
                        second = second.Insert(0, minus);
                    }
                    SetSecondNum(second, true);
                }
            }
        }

        private void SetTottle()
        {
            double firstvalue, secondvalue;

            if (!IsFirst && double.TryParse(first, out firstvalue) && double.TryParse(second, out secondvalue))
            {
                switch (actiontype)
                {
                    case puls:
                        {
                            viewtottle = (firstvalue + secondvalue).ToString();
                            break;
                        }

                    case minus:
                        {
                            viewtottle = (firstvalue - secondvalue).ToString();
                            break;
                        }

                    case multiply:
                        {
                            viewtottle = (firstvalue * secondvalue).ToString();
                            break;
                        }

                    case divide:
                        {
                            viewtottle = (firstvalue / secondvalue).ToString();
                            break;
                        }

                    default: break;
                }

                SetViewText(true);
                actiontype = string.Empty;
                first = viewtottle;
                second = string.Empty;
            }
        }

        //@+id/buttondelete

        private void JudgeSetValue(string num)
        {
            if (IsFirst)
            {
                SetFirstNum(num);
            }
            else
            {
                SetSecondNum(num);
            }
        }

        private void SetFirstNum(string str, bool isforce = false)
        {
            var value = isforce ? str : first + str;
            double v = 0;
            if (double.TryParse(value, out v))
            {
                first = value;
                this.textViewviewtext.Text = first;
                this.textViewtottletext.Text = value;
            }
        }

        private void SetSecondNum(string str, bool isforce = false)
        {
            var value = isforce ? str : second + str;
            double v = 0;
            if (double.TryParse(value, out v))
            {
                second = value;

                SetViewText();
                this.textViewtottletext.Text = value;
            }
        }

        private void SetViewText(bool isresault = false)
        {
            this.textViewviewtext.Text = first.ToString() + Space + actiontype + Space + second.ToString() + (isresault ? Space + result : string.Empty);
            this.textViewtottletext.Text = viewtottle;
        }

        private void ViewClear()
        {
            this.textViewviewtext.Text = viewtext = string.Empty;
            this.textViewtottletext.Text = viewtottle = DefaultView;

            first = string.Empty;
            second = string.Empty;
            actiontype = string.Empty;
        }

    }
}

